package com.ykht.proj.coupon.controller.b;

import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.LookRoleTypeEnum;
import com.ykht.proj.coupon.entity.CnBrand;
import com.ykht.proj.coupon.entity.FxConfShop;
import com.ykht.proj.coupon.service.CnUserExtService;
import com.ykht.proj.coupon.service.FxConfRewardService;
import com.ykht.proj.coupon.vo.FxConfRewardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * @Author: lazyBoy
 * @Date: 2021/2/22 16:22
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/b/fxconfreward")
@Api(value = "/b/fxconfreward", tags = "配置表管理")
public class BxConfRewardController {

	private final FxConfRewardService fxConfRewardService;
	private final CnUserExtService cnUserExtService;

	/**
	 * 分页查询
	 *
	 * @param fxConfRewardVO 品牌邀请、转发、核销配置表
	 * @return
	 */
	@ApiOperation(value = "配置列表", notes = "配置列表")
	@GetMapping("/findConfReward")
	//@PreAuthorize("@pms.hasPermission('coupon_fxconfreward_view')" )
	public R getFxConfRewardPage(FxConfRewardVO fxConfRewardVO) {
		log.info("->查询配置列表");
		CnBrand cnBrand = new CnBrand().selectById(SecurityUtils.getSjId());
		return R.ok(fxConfRewardService.findConfReward(fxConfRewardVO, cnBrand));
	}

	/**
	 * 新增品牌邀请、转发、核销配置表
	 *
	 * @param fxConfRewardVO 品牌邀请、转发、核销配置表
	 * @return R
	 */
	@ApiOperation(value = "新增品牌邀请、转发、核销配置表", notes = "新增品牌邀请、转发、核销配置表")
	@SysLog("新增品牌邀请、转发、核销配置表")
	@PostMapping
	public R save(@RequestBody FxConfRewardVO fxConfRewardVO) {
		fxConfRewardVO.setRemark("自定义配置");
		log.info("->新增分销配置{}", fxConfRewardVO);
		fxConfRewardVO.setCreateUser(SecurityUtils.getUserId());
		fxConfRewardVO.setCreateTime(LocalDateTime.now());
		return R.ok(fxConfRewardService.insert(fxConfRewardVO));
	}

	/**
	 * 修改品牌邀请、转发、核销配置表
	 *
	 * @param fxConfRewardVo 品牌邀请、转发、核销配置表
	 * @return R
	 */
	@ApiOperation(value = "修改配置表", notes = "修改配置表")
	@SysLog("修改配置表")
	@PutMapping
//    @PreAuthorize("@pms.hasPermission('coupon_fxconfreward_edit')" )
	public R updateById(@RequestBody FxConfRewardVO fxConfRewardVo) {
		log.info("->修改分销配置{}", fxConfRewardVo);
		CnBrand cnBrand = new CnBrand().selectById(SecurityUtils.getSjId());
		fxConfRewardVo.setUpdateUser(SecurityUtils.getUserId());
		fxConfRewardVo.setUpdateTime(LocalDateTime.now());
		log.info("->品牌身份{}", cnBrand);
		return R.ok(fxConfRewardService.updateConfReward(fxConfRewardVo, cnBrand));
	}

	@ApiOperation(value = "删除配置", notes = "删除配置")
	@SysLog("删除配置")
	@DeleteMapping("/{id}")
//    @PreAuthorize("@pms.hasPermission('coupon_fxconfreward_edit')" )
	public R delConfReward(@PathVariable Long id) {
		return R.ok(fxConfRewardService.delConfReward(id));
	}

	@ApiOperation(value = "获取当前配置下未上架和未进行配置的门店", notes = "获取当前配置下未上架和未进行配置的门店")
	@SysLog("获取当前配置下未上架和未进行配置的门店")
	@GetMapping
	public R queryAllShopByBrandId(FxConfRewardVO fxConfRewardVO) {
		log.info("->获取当前配置下未上架和未进行配置的门店{}", fxConfRewardVO);
		CnBrand cnBrand = new CnBrand().selectById(SecurityUtils.getSjId());
		log.info("->品牌身份{}", cnBrand);
		return R.ok(fxConfRewardService.queryAllShopByBrandId(cnBrand, fxConfRewardVO));
	}


	@ApiOperation(value = "获取当前配置下所有的门店", notes = "获取当前配置下所有的门店")
	@SysLog("获取当前配置下所有的门店")
	@GetMapping("/updateShowHave")
	public R updateShowHave(String ids) {
		log.info("->获取当前配置下所有的门店{}", ids);
		CnBrand cnBrand = new CnBrand().selectById(SecurityUtils.getSjId());
		String[] arr = ids.split(",");
		return R.ok(fxConfRewardService.updateShowHave(cnBrand, arr));
	}

	@ApiOperation(value = "判断当前配置是否符合规范", notes = "判断当前配置是否符合规范")
	@SysLog("判断当前配置是否符合规范")
	@GetMapping("/judgeConfIsOk")
	public R judgeConfIsOk(String ids, FxConfRewardVO fxConfRewardVO) {
		log.info("->判断当前配置是否符合规范{}", ids);
		log.info("->其他参数{}", fxConfRewardVO);
		CnBrand cnBrand = new CnBrand().selectById(SecurityUtils.getSjId());
		String[] arr = ids.split(",");
		List<FxConfShop> list = fxConfRewardService.judgeConfIsOk(cnBrand, arr, fxConfRewardVO);
		if (list.size() > 0) {
			return R.failed();
		}
		return R.ok();
	}

	@ApiOperation(value = "判断当前的核销配置是否符合规范 保存", notes = "判断当前的核销配置是否符合规范 保存")
	@SysLog("判断当前的核销配置是否符合规范 保存")
	@GetMapping("/judgeConf2IsOk")
	public R judgeConf2IsOk(String ids, FxConfRewardVO fxConfRewardVO) {
		log.info("->判断当前的核销配置是否符合规范 保存{}", fxConfRewardVO);
		CnBrand cnBrand = new CnBrand().selectById(SecurityUtils.getSjId());
		log.info("->品牌身份{}", cnBrand);
		String[] arr = ids.split(",");
		List<FxConfShop> list = fxConfRewardService.judgeConf2IsOk(cnBrand, arr, fxConfRewardVO);
		if (list.size() > 0) {
			return R.failed();
		}
		return R.ok();
	}


}
